package com.atzhou.controller;

import com.atzhou.entity.Department;
import com.atzhou.entity.Employees;
import com.atzhou.service.DepartmentServeice;
import com.atzhou.service.EmployeeService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@Controller
public class HelloController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentServeice departmentServeice;
    
    @RequestMapping("/emps")
    public String emps(@RequestParam(value = "id",defaultValue = "1")Integer pn,Model model){

        PageHelper.startPage(pn,6);
        List<Employees> list = employeeService.getAll();

        PageInfo<Employees> info = new PageInfo<>(list, 6);
        System.out.println("当前页码："+info.getPageNum());
        System.out.println("总页码："+info.getPages());
        System.out.println("总记录数："+info.getTotal());
        System.out.println("当前页有几条记录："+info.getSize());
        System.out.println("当前页的pageSize："+info.getPageSize());
        System.out.println("前一页："+info.getPrePage());
        System.out.println("结果："+info.getList());//查询结果
        int[] nums = info.getNavigatepageNums();
        model.addAttribute("pageInfo",info);
        return "list";
    }
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    public String delById(@PathVariable("id")Integer id){
        System.out.println(id+"==========>");
        employeeService.delById(id);
        return  "redirect:/emps";
    }

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public String upById(@PathVariable("id")Integer id,Model model){
        Employees employees = employeeService.putEmployeeById(id);
        model.addAttribute("employee",employees);
        List<Department> departmentList = departmentServeice.selAll();
        model.addAttribute("depts",departmentList);
        System.out.println("==========>");
        return "upEmployee";
    }


    @RequestMapping(value = "/emp/{id}",method = RequestMethod.POST)
    public String upEmployee(@ModelAttribute("employee") Employees employees){
        System.out.println("要修改的员工：" + employees);
        employees.setdId(employees.getDepartment().getId());
        employeeService.upEmpById(employees);
        return "redirect:/emps";
    }

    @ModelAttribute
    public void getDeAll(@RequestParam(value = "id", required = false) Integer id,
                         Model model){
        if (id != null) {
            Employees employee = employeeService.putEmployeeById(id);
            model.addAttribute("employee", employee);
        }
        // 1、先查出所有部门
        Collection<Department> departments = departmentServeice.selAll();
        // 2、放在请求域中
        model.addAttribute("depts", departments);
    }
    @RequestMapping(value = "/addEmployee")
    public String addEmp(Model model){
        model.addAttribute("employee",new Employees());
        return "addEmp";
    }
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public String putEmp(@Valid Employees employees, BindingResult bindingResult, Model model){
//        System.out.println(10/0);
        System.out.println("要添加的员工：" + employees);
        model.addAttribute("employee",employees);
//        return "success";
        // 获取是否有校验错误
        boolean hasErrors = bindingResult.hasErrors();
        Map<String, Object> errorsMap = new HashMap<String, Object>();
        if (hasErrors) {
            List<FieldError> errors = bindingResult.getFieldErrors();
            System.out.println(errors.size()+"===>");
            Object[] objects = errors.toArray();
            System.out.println(objects.toString());
            for (FieldError fieldError : errors) {
                System.out.println("错误消息提示：" + fieldError.getDefaultMessage());
                System.out.println("错误的字段是？" + fieldError.getField());
                System.out.println(fieldError);
                System.out.println("------------------------");
                errorsMap.put(fieldError.getField(),
                        fieldError.getDefaultMessage());
            }
            model.addAttribute("errorInfo", errorsMap);
            return "addEmp";
        }else{
            employees.setdId(employees.getDepartment().getId());
            employeeService.addEmployee(employees);
            return "redirect:/emps";
        }

    }
}
